#include <iostream>
#include <vector>
#include <Vector.hpp>
using namespace std;

class Solution
{
private:
    enum Direction
    {
        left,
        right,
        top,
        bottom
    };

public:
    vector<int> spiralOrder(vector<vector<int>> &matrix)
    {
        std::vector<int> result;
        int left = 0;
        int right = matrix[0].size() - 1;
        int top = 0;
        int bottom = matrix.size() - 1;
        int x = 0, y = 0;
        Direction dir = Direction::right;
        if (matrix[0].size() == 1)
        {
            dir = Direction::bottom;
        }
        int count = 0;
        while (true)
        {
            result.push_back(matrix[y][x]);
            count++;
            if (count == matrix.size() * matrix[0].size())
            {
                break;
            }
            switch (dir)
            {
            case Direction::right:
                x++;
                if (x == right)
                {
                    dir = Direction::bottom;
                    top++;
                }
                break;
            case Direction::left:
                x--;
                if (x == left)
                {
                    dir = Direction::top;
                    bottom--;
                }
                break;
            case Direction::top:
                y--;
                if (y == top)
                {
                    dir = Direction::right;
                    left++;
                }
                break;
            case Direction::bottom:
                y++;
                if (y == bottom)
                {
                    dir = Direction::left;
                    right--;
                }
                break;
            }
        }
        return result;
    }
};

int main()
{
    cout << "Code54" << endl;
    Solution s;
    // std::vector<std::vector<int>> matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    std::vector<std::vector<int>> matrix = {{3}, {2}};
    std::vector<int> result = s.spiralOrder(matrix);
    PrintVector(result);
    return 0;
}